<template>
  <view style="font-size:14px">
    <public-header :title="config.title"></public-header>

    <rList class="retail-list" mainType="quickInStcok" ref="rList" :selection="true" :classStyle="classStyle" :columnRowConfig="columnRowConfig"
      :page="config.page" :config="config" @on-grid-button="onGridButton" @on-list-item-checked="onItemChecked"
      :searchValKey="searchValKey" :condition="condition" style="height:calc(100vh - 6rem)" @table-loaded="tableLoaded">
    </rList>


    <uni-popup ref="popup" type="dialog">
      <uni-popup-dialog type="info" title="确认操作" @close="close" @confirm="confirm"></uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script>
  import rList from '@/components/retail-list/retail-list.vue'
  import lockscreen from '@/common/mixins/lockscreen.js'
  export default {
    mixins: [lockscreen],
    data() {
      return {
        classStyle: "retail-list-right height-full",
        config: {
          showRowButton:true,
          serviceName: "srvretail_member_info_select",
          deleteService: "srvretail_warehousing_delete",
          updateSrv: "",
          listLayout: "listAndFilter", // listAndDetail listAndFilter  list
          detailPath: "simple", //  simple  基础的； 或者配置页面路由 路由格式待定
          page: {
            pageNo: 1,
            rownumber: 10
          },
          listHeaders: [''],
          showBatchSubmit: 'warehousing_state',
          title: "",
          sumColumns: {
            num: "warehousing_num",
            amount: "warehousing_amount",
            state: "warehousing_state"
          },
          tabsType: "custom", //
          custTabs: [{
            "colName": "warehousing_no",
            "type": "input",
            "inputType": "String",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "入库单号",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "create_time",
            "type": "between",
            "inputType": "BetweenDate",
            "parent": "",
            "is_count": true,
            "show_expr": {},
            "label": "日期",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": true
          }, {
            "colName": "warehousing_state",
            "type": "Select",
            "inputType": "enum", // enum dict fk
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "状态",
            "showLabel": true,
            "colspan": 24,
            "default": "",
            "showAllTag": true,
            "tags": [],
            "options": {},
            "appendSearchButton": true
          }],
          tabs: {
            layout: 'inline', // 'inline'   block
          }
        },
        detailData: null,
        condition: [],
        searchVal: "",
        searchValKey: "member_name,member_no,order_no", // 查询的字段名  "col,col,col"
        columnRowConfig: [],
        gridActionQuery: null
      }
    },
    components: {
      rList
    },
    methods: {
      onItemChecked(e) {
        console.log("onItemChecked", e)
        // this.detailData = JSON.stringify(e)
        this.toStockBatchSubmit(e)
      },
      toStockBatchSubmit(e) {

        let url =
          `/pages/main/stockmgt/stock-batch-submit?serviceName=srvretail_warehousing_update&deleteService=${this.config.deleteService}&type=update&id=${e.id}&title=${e.warehousing_no}&childKeyCol=warehousing_no&titleText=快速入库`
        if (e.warehousing_state == '已提交') {
          url =
            `/pages/main/stockmgt/stock-batch-submit?serviceName=srvretail_warehousing_select&type=detail&id=${e.id}&title=${e.warehousing_no}&childKeyCol=warehousing_no&titleText=快速入库`
        }
        if (e.operator_name) {
          url += `&operator_name=${e.operator_name}`
        }
        if (e.submit_time) {
          url += `&submit_time=${e.submit_time}`
        }
        if (e.create_time) {
          url += `&create_time=${e.create_time}`
        }
        if (e.warehousing_state) {
          url += `&state=${e.warehousing_state}`
        }
        uni.navigateTo({
          url: url
        })
      },
      requestBatchSubmit() {
        let self = this
        let e = this.gridActionQuery
        let datas = e.datas
        let waitIds = e.ids
        let req = []
        if (e.type && ['batch-submit', 'batch-submit-checked'].includes(e.type)) {
          let srv = this.config?.updateSrv || this.config.serviceName.replace("select", 'update')
          let listData = datas.filter(item => item.warehousing_state == '待提交')
          let ids = listData.map(item => item.id)
          if(Array.isArray(waitIds)&&waitIds.length>0){
            ids = waitIds
          }
          console.log(ids)
          console.log(this.config?.updateSrv);
          if (ids.length === 0) {
            uni.showToast({
              title: '请选择待提交的数据进行提交',
              icon: 'none'
            })
            return
          }
          for (let id of ids) {
            req.push({
              "serviceName": srv,
              "condition": [{
                "colName": "id",
                "ruleType": "eq",
                "value": id
              }],
              "data": [{
                "warehousing_state": "已提交"
              }]
            })
          }
          self.$fetch('operate', srv, req).then((res) => {
            if (res.success) {
              uni.showToast({
                icon: "none",
                title: res.resultMessage
              })
              self.$refs.rList.reload()
            } else {
              uni.showToast({
                icon: "none",
                title: res.resultMessage
              })
            }
          })
        }
      },
      onGridButton(e) {
        let self = this
        console.log('onGridButton', e)
        this.gridActionQuery = e
        this.open()

      },
      open() {
        this.$refs.popup.open()
      },
      /**
       * 点击取消按钮触发
       * @param {Object} done
       */
      close() {
        // TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
        // ...
        this.$refs.popup.close()
      },
      /**
       * 点击确认按钮触发
       * @param {Object} done
       * @param {Object} value
       */
      confirm(value) {
        // 输入框的值
        console.log(value)
        // TODO 做一些其他的事情，手动执行 close 才会关闭对话框
        // ...
        this.requestBatchSubmit()
        this.$refs.popup.close()
      },
      tableLoaded(state) {
        console.log(state)
      }
    },
    onLoad(option) {
      // console.error("option 加载",option)
      this.config.serviceName = option.srv
      this.config.updateSrv = option.updateSrv
      this.config.listLayout = option.listLayout
      this.config.pageType = option.pageType
      this.config.title = option.title

      // uni.getSystemInfo({
      //   success: (res) => {
      //     // console.log(JSON.stringify(res), "systemInfo")
      //   }
      // })
      // console.log('getCurrentPages() ', getCurrentPages())
    },
    mounted() {
      console.log(this.$refs, "==das==")
      console.log(this.$refs.gg, "==this.$refs.gg==")
    },
    onShow(option) {
      this.gridActionQuery = null
      if (this.$refs.hasOwnProperty('rList')) {

        this.$refs.rList.reload()
      }

    }
  }
</script>

<style lang="scss" scoped>
  .list-detail {
    border-radius: 10rpx;
    background-color: #fff;
    box-shadow: #333;
  }

  ::v-deep .retail-list {
    height: calc(100vh - 6rem);

    .retail-list {
      height: calc(100vh - 6rem);
    }
  }
</style>
